package com.tianze.tzkf.common;

import android.app.Activity;
import android.content.ComponentName;
import android.os.Build;
import android.util.Log;

import java.lang.ref.WeakReference;
import java.util.Iterator;
import java.util.Stack;

import static android.content.Context.ACTIVITY_SERVICE;

/**
 * Created by Zcyang on 2018/8/9.
 */
public class ActivityStackManager {

    private static ActivityStackManager sInstance = new ActivityStackManager();
    private WeakReference<Activity> topAttachedActivityWeakRef;
    private static Stack<Activity> mActivityStack = new Stack();

    private ActivityStackManager() {
    }

    public static ActivityStackManager getInstance() {
        return sInstance;
    }

    public Activity getTopActivity() {
        Activity currentActivity = null;
        if (mActivityStack.size() > 0) {
            currentActivity = (Activity)mActivityStack.get(mActivityStack.size() - 1);
        }

        return currentActivity;
    }

    public Activity getTopActivity(Class activityExpected) {
        Activity activity = this.getTopActivity();
        return activity != null && activity.getClass().equals(activityExpected) ? activity : null;
    }

    public Activity getActivity(Class activityExpected) {
        if (mActivityStack == null) {
            return null;
        } else {
            Iterator var2 = mActivityStack.iterator();

            Activity activity1;
            do {
                if (!var2.hasNext()) {
                    return null;
                }

                activity1 = (Activity)var2.next();
            } while(!activity1.getClass().equals(activityExpected));

            return activity1;
        }
    }

    public boolean finishActivityByIdx(int idxTop) {
        if (mActivityStack.isEmpty()) {
            return false;
        } else {
            int index = mActivityStack.size() - idxTop;
            Activity activity = (Activity)mActivityStack.remove(index);
            if (activity != null && !activity.isFinishing()) {
                activity.finish();
                return true;
            } else {
                return true;
            }
        }
    }

    public void setTopAttached(Activity activity) {
        this.topAttachedActivityWeakRef = new WeakReference(activity);
    }

    public void removeTopAttached(Activity activity) {
        if (activity != null) {
            if (this.topAttachedActivityWeakRef != null) {
                Activity activity1 = (Activity)this.topAttachedActivityWeakRef.get();
                if (activity.equals(activity1)) {
                    this.topAttachedActivityWeakRef = null;
                }
            }

        }
    }

    public Activity getTopAttached() {
        if (this.topAttachedActivityWeakRef != null) {
            Activity activity = (Activity)this.topAttachedActivityWeakRef.get();
            if (isUsable(activity)) {
                return activity;
            }
        }

        return null;
    }

    public static boolean isUsable(Activity activity) {
        if (activity == null) {
            return false;
        } else if (activity.isFinishing()) {
            return false;
        } else {
            return Build.VERSION.SDK_INT < 17 || !activity.isDestroyed();
        }
    }

    public int size() {
        return mActivityStack.size();
    }

    public void addActivity(Activity activity) {
        if (mActivityStack == null) {
            mActivityStack = new Stack();
        }

        if (activity != null) {
            mActivityStack.add(activity);
            Log.e("dialog", "mActivityStack.size()--addActivity:" + activity.getClass().getSimpleName() + mActivityStack.size());
        }

    }

    public void removeActivity(Activity activity) {
        mActivityStack.remove(activity);
        Log.e("dialog", "mActivityStack.size()--removeActivity:" + activity.getClass().getSimpleName() + mActivityStack.size());
    }

    public boolean isActivityAlive(Class<?> cls) {
        if (mActivityStack == null) {
            return false;
        } else {
            Iterator var2 = mActivityStack.iterator();

            Activity activity1;
            do {
                if (!var2.hasNext()) {
                    return false;
                }

                activity1 = (Activity)var2.next();
            } while(!activity1.getClass().equals(cls));

            return true;
        }
    }

    public void finishActivity(Activity activity) {
        if (mActivityStack != null) {
            if (activity != null) {
                mActivityStack.remove(activity);
                activity.finish();
            }

        }
    }

    public void finishActivity(Class<?> cls) {
        if (mActivityStack != null) {
            Iterator var2 = mActivityStack.iterator();

            while(var2.hasNext()) {
                Activity activity = (Activity)var2.next();
                if (activity.getClass().equals(cls)) {
                    this.finishActivity(activity);
                }
            }

        }
    }

    public Activity currentActivity(){
        Activity activity= mActivityStack.lastElement();
        return activity;
    }



}
